import { messages, isEmptyObj } from "share/common";
import React, { Component } from "react";
import { Button, Form, Spin, Affix } from "antd";
import AirServer from "../airline-server";
import AirLineUtil from "../airline-config-util";
import SecondCheckbox from "../components/second-checkbox-form";
import { message } from "antd/lib/index";

const FormItem = Form.Item;

@Form.create()
class SetRefundChange extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      settingData: {},
    };
  }

  componentWillMount() {
    this.initData();
  }

  initData = () => {
    AirServer(this)
      .getAirModelData({ companyOid: this.props.companyOID })
      .then((res) => {
        this.setState({ settingData: res.data.body }, this.props.form.resetFields);
      });
  };

  onSaveSetting = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const { settingData } = this.state;
        const params = AirLineUtil.formatRefundChangeData(settingData, values);
        params.companyOid = this.props.companyOID;
        params.voluntaryRefundReason = null;
        params.involuntaryRefundReason = null;
        params.voluntaryChangeReason = null;
        params.involuntaryChangeReason = null;
        AirServer(this)
          .saveAirModelData(params)
          .then((res) => {
            if (res.data.success) {
              this.initData();
              message.success(messages("supplier-1.key355") /*保存成功*/);
            } else {
              message.error(res.data.message);
            }
          });
      }
    });
  };

  render() {
    const { loading, settingData } = this.state;
    const { getFieldDecorator } = this.props.form;
    return (
      <div className="airline-config-container">
        <Spin className="spin" spinning={loading}>
          {!isEmptyObj(settingData) && (
            <Form>
              <h2>{messages("supplier-1.key382") /*改签方式*/}</h2>
              <a>{messages("supplier-1.key383") /*优行商旅暂不支持事前审批，若选择，会自动略过事前审批环节*/}</a>
              <a className="a-line-bottom">{messages("supplier-1.key384") /*审批流程可在表单管理进行设置*/}</a>
              <FormItem>
                {getFieldDecorator("changeWay", {
                  initialValue: settingData.changeWay ? settingData.changeWay.split(",") : [],
                })(AirLineUtil.getRefundChangeFormComp("changeWay"))}
              </FormItem>
              <br />
              <br />
              <h2>{messages("supplier-1.key385") /*退票方式*/}</h2>
              <a className="a-line-bottom">{messages("supplier-1.key384") /*审批流程可在表单管理进行设置*/}</a>
              <FormItem>
                {getFieldDecorator("refundWay", {
                  initialValue: settingData.refundWay ? settingData.refundWay.split(",") : [],
                })(AirLineUtil.getRefundChangeFormComp("refundWay"))}
              </FormItem>
              <br />
              <br />
              <h2>{messages("supplier-1.key386") /*退改签费用确认方式*/}</h2>
              <a className="a-line-bottom">
                {
                  messages(
                    "supplier-1.key387"
                  ) /*员工在提交退改申请时，若服务商“未显示／提供预计费用”，在员工提交退改申请后10分钟内，服务商会返回具体退改费用，员工可选择继续退改或取消退改。默认退票无需员工自行确认，由系统自动进行确认 */
                }
              </a>
              <FormItem>
                {getFieldDecorator("activeConfirmRefund", {
                  initialValue: settingData.activeConfirmRefund ? ["1"] : [],
                })(AirLineUtil.getRefundChangeFormComp("refundConfirmWay"))}
              </FormItem>
              <br />
              <br />
              <h2>{messages("supplier-1.key388") /*自愿改签原因*/}</h2>
              <a className="a-line-bottom">{messages("supplier-1.key389") /*未启用时，员工提交时无需选择理由*/}</a>
              <FormItem>
                {getFieldDecorator("change_root", {
                  initialValue: {
                    enabledVoluntaryChangeReason: settingData.enabledVoluntaryChangeReason,
                    voluntaryChangeReasonList: settingData.voluntaryChangeReasonList || [],
                  },
                })(
                  <SecondCheckbox
                    {...AirLineUtil.getRefundChangeFormComp("change")}
                    companyOID={this.props.companyOID}
                  />
                )}
              </FormItem>
              <br />
              <br />
              <h2>{messages("supplier-1.key390") /*非自愿改签原因*/}</h2>
              <a className="a-line-bottom">{messages("supplier-1.key389") /*未启用时，员工提交时无需选择理由*/}</a>
              <FormItem>
                {getFieldDecorator("unChange_root", {
                  initialValue: {
                    enabledInvoluntaryChangeReason: settingData.enabledInvoluntaryChangeReason,
                    involuntaryChangeReasonList: settingData.involuntaryChangeReasonList || [],
                  },
                })(
                  <SecondCheckbox
                    {...AirLineUtil.getRefundChangeFormComp("unChange")}
                    companyOID={this.props.companyOID}
                  />
                )}
              </FormItem>
              <br />
              <br />
              <h2>{messages("supplier-1.key391") /*自愿退票原因*/}</h2>
              <a className="a-line-bottom">{messages("supplier-1.key389") /*未启用时，员工提交时无需选择理由*/}</a>
              <FormItem>
                {getFieldDecorator("refund_root", {
                  initialValue: {
                    enabledVoluntaryRefundReason: settingData.enabledVoluntaryRefundReason,
                    voluntaryRefundReasonList: settingData.voluntaryRefundReasonList || [],
                  },
                })(
                  <SecondCheckbox
                    {...AirLineUtil.getRefundChangeFormComp("refund")}
                    companyOID={this.props.companyOID}
                  />
                )}
              </FormItem>
              <br />
              <br />
              <h2>{messages("supplier-1.key392") /*非自愿退票原因*/}</h2>
              <a className="a-line-bottom">{messages("supplier-1.key389") /*未启用时，员工提交时无需选择理由*/}</a>
              <FormItem>
                {getFieldDecorator("unRefund_root", {
                  initialValue: {
                    enabledInvoluntaryRefundReason: settingData.enabledInvoluntaryRefundReason,
                    involuntaryRefundReasonList: settingData.involuntaryRefundReasonList || [],
                  },
                })(
                  <SecondCheckbox
                    {...AirLineUtil.getRefundChangeFormComp("unRefund")}
                    companyOID={this.props.companyOID}
                  />
                )}
              </FormItem>
            </Form>
          )}

          <br />
          <br />
          <br />
          <br />
          <br />
          <br />
        </Spin>
        <Affix offsetBottom={0} className="setting-fotter-class">
          <Button style={{ marginLeft: 20 }} type="primary" onClick={this.onSaveSetting}>
            {messages("supplier-1.key356") /* 保存配置*/}
          </Button>
        </Affix>
      </div>
    );
  }
}

export default SetRefundChange;
